package de.clapp.client.qrcodes;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.JXTable;


public class QRCodeTableSelectionListener implements ListSelectionListener
{
    private final QRTableModel tableModel;

    private CenterPanel cp;

    private JXTable table;

    public QRCodeTableSelectionListener(final QRTableModel tableModel, JXTable table, CenterPanel cp)
    {
        this.tableModel = tableModel;
        this.table = table;
        this.cp = cp;
    }

    @Override
    public void valueChanged(ListSelectionEvent arg0)
    {
        int index = this.table.getSelectedRow();
        String qrText = (String) this.tableModel.getValueAt(index, 1);
        try
        {
            File imageFile = QRCodeGenerator.createQRCode(qrText);
            cp.imageChanged(ImageIO.read(imageFile));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
